package com.xzg.erp.process.web;

import com.xzg.erp.process.service.MyTaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/task")
public class MyTaskController {

    @Autowired
    private MyTaskService myTaskService;

    @GetMapping("/page/tasks")
    public ResponseEntity pageTasks() {
        return ResponseEntity.ok(myTaskService.pageTasks());
    }

    @GetMapping("/tasks/{assignee}")
    public List<MyProcessController.TaskRepresentation> getTasks(@PathVariable String assignee) {
        List<Task> tasks = myTaskService.getTasks(assignee);
        List<MyProcessController.TaskRepresentation> dtos = new ArrayList<MyProcessController.TaskRepresentation>();
        for (Task task : tasks) {
            dtos.add(new MyProcessController.TaskRepresentation(task.getId(), task.getName()));
        }
        return dtos;
    }
}
